Bitwise XOR Operator (^)
The ^ operator (also known as the XOR operator) stands for Exclusive Or. Here, if bits in the compared position do not match their resulting bit is 1. i.e, The result of the bitwise XOR operator is 1 if the corresponding bits of two operands are opposite, otherwise 0.
Example:
Take two bit values X and Y, where X = 7= (111)2 and Y = 4 = (100)2 . Take Bitwise and of both X & y
Explanation: On the basis of truth table of bitwise XOR operator we can conclude that the result of
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0We used the similar concept of bitwise operator that are show in the image.
Implementation of XOR operator:
#include <iostream>
using namespace std;
int main()
{
int a = 12, b = 25;
cout << (a ^ b);
return 0;
}
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 12, b = 25;
int result = a ^ b;
System.out.println(result);
}
}
// This code is contributed by garg28harsh.
a = 12
b = 25
result = a ^ b
print(result)
# This code is contributed by garg28harsh.
// C# Code
using System;
public class GFG {
static public void Main()
{
// Code
int a = 12, b = 25;
int result = a ^ b;
Console.WriteLine(result);
}
}
// This code is contributed by lokesh
let a = 12;
let b = 25;
console.log((a ^ b));
// This code is contributed by akashish__
Output
21
Time Complexity: O(1)
Auxiliary Space: O(1)
Introduction to Bitwise Algorithms – Data Structures and Algorithms Tutorial
Bit stands for binary digit. A bit is the basic unit of information and can only have one of two possible values that is 0 or 1. In our world, we usually with numbers using the decimal base. In other words. we use the digit 0 to 9 However, there are other number representations that can be quite useful such as the binary number systems.
Unlike humans, computers have no concepts of words and numbers. They receive data encoded at the lowest level as a series of zeros and ones (0 and 1). These are called bits, and they are the basis for all the commands they receive. We’ll begin by learning about bits and then explore a few algorithms for manipulating bits. We’ll then explore a few algorithms for manipulating bits. The tutorial is meant to be an introduction to bit algorithms for programmers.
Table of Content
- What is Bitwise Algorithms?
- Bitwise Operators / Basics of Bit manipulation
- Bitwise AND Operator (&)
- Bitwise OR Operator (|)
- Bitwise XOR Operator (^)
- Bitwise NOT Operator (!~)
- Left-Shift (<<)
- Right-Shift (>>)
- Application of Bit Operators
- Important Practice Problems on Bitwise Algorithm
Contact Us